<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/3
 * Time: 11:37
 */

namespace app\common\controller;


class Sms
{
    function SendSms($phone,$param,$type)
    {
        // 配置信息
        import('Dayu.Dayu');

        //获取短信配置
        if($type==0){
            $data = \think\Db::name('BydSmsconfig')->where('id','3')->find();

        }else{
            $data = \think\Db::name('BydSmsconfig')->where('id','4')->find();
        }
        $dayuApi = new \Dayu($data['appkey'],$data['secretkey'],$data['name'],$data['temp_id']);

        $backs = $dayuApi->sendSms($phone,$param);

        if($backs['Message'] =='OK'){
            return [];
        }else{
            return 1;
        }
    }
    /**发送验证码
     * @param $phone  电话号码
     * @return array
     */
    public function sendCode($phone,$type,$code)
    {

        //if(!checkPhone($phone))  return ['code'=>1,'messgae'=>'手机号码错误'];
       if($type==0){
           $number = $code;
       }else{
           $number=$code;
       }


        $qq=cache('code'.$phone,$number,900);

        $backCode = $this->SendSms($phone,$number,$type);

        if(!empty($backCode)) {
            return ['code'=>0,'messgae'=>'网络错误'];
        } else {
            return ['code'=>1,'messgae'=>'成功','code_num'=>''];
        }
    }
    /**发送验证码
     * @param $phone  电话号码
     * @return array
     */
    public function sendCodevr($phone,$vr)
    {

        //if(!checkPhone($phone))  return ['code'=>1,'messgae'=>'手机号码错误'];
//       if($type==0){
        $number = $vr;
//       }else{
//           $number=$code;
//       }


        $qq=cache('code'.$phone,$number,900);

        $backCode = $this->SendSms($phone,$number);

        if(!empty($backCode)) {
            return ['code'=>0,'messgae'=>'网络错误'];
        } else {
            return ['code'=>1,'messgae'=>'成功','code_num'=>''];
        }
    }
    /**检测验证码是否正确
     * @param $phone
     * @param $code
     */
    public function TestingCode($phone,$code)
    {
        $yuanCode = \think\Cache::get('code'.$phone);
        //$yuanCode = cache('code'.$phone);
        if($yuanCode !=$code || !$yuanCode || !$code){
            return ['code'=>0,'messgae'=>'验证码错误'];
        }
        cache('code'.$phone,null);
        return ['code'=>1,'messgae'=>'验证成功'];
    }
}